<template>
	<view class="item" @click="toDetail(item.id)">
		<view class="title text-ellipsis"><u-tag v-if="item.categoryName" :text="item.categoryName" mode="light"
				shape="circleLeft" type="info" />
			<view v-html="item.title"></view>
		</view>
		<view class="title-image">
			<image v-if="item.thumbnail" :mode="aspectFill" :src="item.thumbnail"></image>
			<image v-else :mode="aspectFill"
				src="https://zlw-blog.oss-cn-hangzhou.aliyuncs.com/2024/08/13/notfound.png"></image>
		</view>
		<view class="content flex-row">
			<view class="info">
				<view class="summary" v-html="item.summary"></view>
				<view class="flex-row">
					<text class="views">朱力维</text>
					<text v-if="!item.looktime" class="date">{{ showInitDate(item.createTime, 'all') }}</text>
					<text v-else class="date">浏览时间：{{ showInitDate(item.looktime, 'all') }}</text>
				</view>
			</view>
			<!-- 		<view class="cover">
				<image class="img"
					src="https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1851283359,3457678391&fm=26&gp=0.jpg">
				</image>
			</view> -->
		</view>
	</view>
</template>

<script>
	import {
		parseTime
	} from "../../utils/tool"
	import {
		updateViewCount
	} from '../../api/article'
	import {
		initDate
	} from '../../utils/common'
	export default {
		props: {
			item: {
				type: Object,
				default () {
					return {
						thumbnail: 'https://zlw-blog.oss-cn-hangzhou.aliyuncs.com/2024/08/13/notfound.png'
					}
				}
			}
		},
		data() {
			return {}
		},
		onLoad: {

		},
		methods: {
			showInitDate: function(oldDate, full) {
				return initDate(oldDate, full)
			},
			toDetail(id) {
				updateViewCount(id)
				this.saveHistory()
				uni.navigateTo({
					url: `/pages/article/detail?id=${id}`
				})
			},
			// 存储历史记录
			saveHistory() {
				let historyArr = uni.getStorageSync("historyArr") || []
				this.item.looktime = parseTime(Date.now())

				// 去重
				let index = historyArr.findIndex(i => {
					return i.id == this.item.id
				})

				if (index >= 0) {
					historyArr.splice(index, 1)
				}
				// unshift：数组向前追加
				historyArr.unshift(this.item)
				historyArr = historyArr.slice(0, 10)
				uni.setStorageSync("historyArr", historyArr)
			},
		}
	}
</script>

<style lang="scss" scoped>
	.item {
		.title-image {
			image {
				width: 100%;
				height: 250rpx;
			}
		}

		padding: 40rpx;
		margin-bottom: 30rpx;
		background-color: #FFF;

		.title {
			font-weight: bold;
			padding-bottom: 30rpx;
			border-bottom: 2rpx solid #F5F5F5;
			display: flex;

			text {
				font-size: 40rpx;
			}
		}

		.content {
			padding-top: 30rpx;
			align-items: flex-start;

			.info {
				width: 100%;

				.summary {
					color: #777;
					height: 80rpx;
					font-size: 24rpx;
					line-height: 1.6;
					margin-bottom: 10rpx;
					@include text-ellipsis(2);
				}

				.date {
					font-size: 24rpx;
					color: $main-color;
					opacity: 0.6;
				}

				.views {
					color: #999;
					font-size: 24rpx;
				}
			}

			.cover {
				width: 140rpx;
				height: 120rpx;

				.img {
					width: 100%;
					height: 100%;
					border-radius: 4rpx;
				}
			}
		}
	}
</style>